package com.navi.loaddata.job;

import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.IEtlConfServices;
import com.navi.loaddata.loadService.impl.Dcdata.array.LoadDcDataServiceARRAY;
import com.navi.loaddata.loadService.impl.Dcdata.cell1.LoadDcDataServiceCELL1;
import com.navi.loaddata.loadService.impl.Dcdata.cell2.LoadDcDataServiceCELL2;
import com.navi.loaddata.loadService.impl.Dcdata.cf.LoadDcDataServiceCF;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author jay
 */
@Service
//@DisallowConcurrentExecution//禁止并发
public class LoadDcData implements Job {
    private static final Logger logger = LoggerFactory.getLogger(LoadDcData.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private QuartzManage quartzManage;

    @Autowired
    private LoadDcDataServiceARRAY loadDcDataService;
    @Autowired
    private LoadDcDataServiceCF loadDcDataServiceCF;
    @Autowired
    private LoadDcDataServiceCELL1 loadDcDataServiceCELL1;
    @Autowired
    private LoadDcDataServiceCELL2 loadDcDataServiceCELL2;
    @Autowired
    private IEtlConfServices etlConfServices;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        EtlConfD etlConfD = (EtlConfD) jobDataMap.get("etlConf");
        etlConfD = etlConfServices.getNewConf(etlConfD);
        if(null==etlConfD) {
            logger.error("找不到该JOB。");
            return;
        }

        if ("N".equals(etlConfD.getRunFlg())) {
            etlConfD = etlConfServices.beginJob(etlConfD);
            if (StringUtils.isEmpty(etlConfD.getQueryMinute())) {
                return;
            }
            startETL(etlConfD);
        } else {
            logger.info("Job {}-{} is running.", etlConfD.getJobGroupName(), quartzManage.getRealJobName(etlConfD));
        }
    }

    private void startETL(EtlConfD etlConfD) {
        int countSize=0;
        try {

            if ("ARRAY".equals(etlConfD.getShop())) {
                countSize = loadDcDataService.loadDcDataMain(etlConfD);
            }else if("CF".equals(etlConfD.getShop())){
                countSize = loadDcDataServiceCF.loadDcDataMain(etlConfD);
            }else if("OC".equals(etlConfD.getShop())){
                if("CELL1".equals(etlConfD.getCellType())) {
                    countSize = loadDcDataServiceCELL1.loadDcDataMain(etlConfD);
                }else if("CELL2".equals(etlConfD.getCellType())){
                    countSize = loadDcDataServiceCELL2.loadDcDataMain(etlConfD);
                }
            }
            etlConfD.setCountSize(countSize);
            etlConfServices.endJob(etlConfD);
            etlConfServices.addHis(etlConfD, null);
        } catch (Exception e) {
            logger.info("Job {} [{}]-[{}] FAIL TO SYNCHRONIZE.", etlConfD.getProjectName() + etlConfD.getJobGroupName() + etlConfD.getJobName(), etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());

            if(null!=e.getMessage()&&e.getMessage().contains("Unable to acquire JDBC Connection")){//无法连接,SLEEP
                etlConfD = etlConfServices.getNewConf(etlConfD);
                etlConfD.setValidFlg("N");
                etlConfD.setRunFlg("N");
                etlConfServices.saveReloadJob(etlConfD);
                try{
                    Thread thread = Thread.currentThread();
                    thread.sleep(60000);//暂停60秒后程序继续执行
                }catch (InterruptedException ex) {
                    logger.error(LoggerUtil.stackTraceToString(ex));
                }
                etlConfD = etlConfServices.getNewConf(etlConfD);
                etlConfD.setValidFlg("Y");
                etlConfServices.saveReloadJob(etlConfD);
                return;
            }

            etlConfD.setCountSize(0);
            etlConfServices.endJob(etlConfD);
            etlConfServices.addHis(etlConfD, e.getMessage());

        }
    }
}
